package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppPolicyNewsService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppPolicyNewsCreateValidate;
import com.mdd.admin.validate.yylq.TAppPolicyNewsSearchValidate;
import com.mdd.admin.validate.yylq.TAppPolicyNewsUpdateValidate;
import com.mdd.admin.vo.yylq.TAppPolicyNewsDetailVo;
import com.mdd.admin.vo.yylq.TAppPolicyNewsListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppPolicyNews;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppPolicyNewsMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * app政策新闻实现类
 * @author LikeAdmin
 */
@Service
public class TAppPolicyNewsServiceImpl implements ITAppPolicyNewsService {
        
    @Resource
    TAppPolicyNewsMapper tAppPolicyNewsMapper;

    /**
     * app政策新闻列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppPolicyNewsListedVo>
     */
    @Override
    public PageResult<TAppPolicyNewsListedVo> list(PageValidate pageValidate, TAppPolicyNewsSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppPolicyNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        tAppPolicyNewsMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:title:str",
            "=:subtitle:str",
            "=:content:str",
            "=:pic:str",
            "=:video:str",
            "=:type:int",
            "=:chargeFlag@charge_flag:int",
            "=:hotFlag@hot_flag:int",
            "=:provinceCode@province_code:str",
            "like:provinceName@province_name:str",
            "=:cityCode@city_code:str",
            "like:cityName@city_name:str",
            "=:areaCode@area_code:str",
            "=:sort:int",
        });

        IPage<TAppPolicyNews> iPage = tAppPolicyNewsMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppPolicyNewsListedVo> list = new LinkedList<>();
        for(TAppPolicyNews item : iPage.getRecords()) {
            TAppPolicyNewsListedVo vo = new TAppPolicyNewsListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * app政策新闻详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppPolicyNews
     */
    @Override
    public TAppPolicyNewsDetailVo detail(Integer id) {
        TAppPolicyNews model = tAppPolicyNewsMapper.selectOne(
                new QueryWrapper<TAppPolicyNews>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppPolicyNewsDetailVo vo = new TAppPolicyNewsDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * app政策新闻新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppPolicyNewsCreateValidate createValidate) {
        TAppPolicyNews model = new TAppPolicyNews();
        model.setTitle(createValidate.getTitle());
        model.setSubtitle(createValidate.getSubtitle());
        model.setContent(createValidate.getContent());
        model.setPic(createValidate.getPic());
        model.setVideo(createValidate.getVideo());
        model.setType(createValidate.getType());
        model.setChargeFlag(createValidate.getChargeFlag());
        model.setHotFlag(createValidate.getHotFlag());
        model.setProvinceCode(createValidate.getProvinceCode());
        model.setProvinceName(createValidate.getProvinceName());
        model.setCityCode(createValidate.getCityCode());
        model.setCityName(createValidate.getCityName());
        model.setAreaCode(createValidate.getAreaCode());
        model.setAreaName(createValidate.getAreaName());
        model.setSort(createValidate.getSort());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        tAppPolicyNewsMapper.insert(model);
    }

    /**
     * app政策新闻编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppPolicyNewsUpdateValidate updateValidate) {
        TAppPolicyNews model = tAppPolicyNewsMapper.selectOne(
                new QueryWrapper<TAppPolicyNews>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setTitle(updateValidate.getTitle());
        model.setSubtitle(updateValidate.getSubtitle());
        model.setContent(updateValidate.getContent());
        model.setPic(updateValidate.getPic());
        model.setVideo(updateValidate.getVideo());
        model.setType(updateValidate.getType());
        model.setChargeFlag(updateValidate.getChargeFlag());
        model.setHotFlag(updateValidate.getHotFlag());
        model.setProvinceCode(updateValidate.getProvinceCode());
        model.setProvinceName(updateValidate.getProvinceName());
        model.setCityCode(updateValidate.getCityCode());
        model.setCityName(updateValidate.getCityName());
        model.setAreaCode(updateValidate.getAreaCode());
        model.setAreaName(updateValidate.getAreaName());
        model.setSort(updateValidate.getSort());
        model.setUpdateTime(new Date());
        tAppPolicyNewsMapper.updateById(model);
    }

    /**
     * app政策新闻删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppPolicyNews model = tAppPolicyNewsMapper.selectOne(
                new QueryWrapper<TAppPolicyNews>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppPolicyNewsMapper.delete(new QueryWrapper<TAppPolicyNews>().eq("id", id));
    }

}
